package cn.wang.service.cargo.Impl;

import cn.wang.dao.BaseDao;

import cn.wang.entity.cargo.Contract;
import cn.wang.service.cargo.ContractService;
import cn.wang.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by john on 2017/11/1.
 */
@Service("contractService")
@Transactional
public class ContractServiceImpl implements ContractService {
    @Autowired
    private BaseDao<Contract, String> baseDao;


    @Override
    public Pagination findPage(int pageNo, int pageSize, int i) {


        return baseDao.pageByHql("from Contract c where c.state=?", pageNo, pageSize, i);
    }

    @Override
    public List<Contract> findAll() {
        return baseDao.getListByHQL("from Contract");
    }

    @Override
    public void save(Contract contract) {
        contract.setTotalAmount(0.0);
        contract.setState(0);
        baseDao.save(contract);
    }

    @Override
    public void update(Contract model) {
        //查询出老数据
        Contract contract = baseDao.get(Contract.class, model.getId());
        model.setCreateTime(contract.getCreateTime());
        model.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        baseDao.evcit(contract);
        baseDao.update(model);
    }

    @Override
    public Contract findById(String id) {
        return baseDao.get(Contract.class, id);
    }

    @Override
    public void delete(String[] ids) {
        //业务层放到service 保证在同一事务
        for (String id : ids) {
            baseDao.deleteById(Contract.class, id);
        }
    }

    @Override
    public void updateById(String[] ids) {
        for (String id : ids) {
            Contract contract = baseDao.get(Contract.class, id);
            contract.setState(1);
            baseDao.update(contract);
        }
    }

    @Override
    public List<Contract> findBydeliveryPeriod(String date) {

        return baseDao.getListByHQL("from Contract c where to_char(c.deliveryPeriod,'yyyy-mm-dd')=?", date);
    }
}
